/* eslint-disable quotes */
<template>
  <!-- 注册页面的整体盒子 -->
  <div class="register-container">
    <!-- 注册的盒子 -->
    <div class="register-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <el-form :model="regForm" :rules="regFormRules" ref="regFormRef">
        <el-form-item prop="username">
          <el-input placeholder="请输入用户名" v-model="regForm.username"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="regForm" :rules="regFormRules">
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" v-model="regForm.password" show-password></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="regForm" :rules="regFormRules">
        <el-form-item prop="repassword">
          <el-input placeholder="请确认密码" v-model="regForm.repassword" show-password></el-input>
        </el-form-item>
      </el-form>
      <el-form>
        <el-form-item>
          <el-button type="primary" @click="handleReg">注册</el-button>
          <el-link type="info" @click="$router.push('/login')">去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { regAPI } from '@/api/register.js'
export default {
  // eslint-disable-next-line quotes
  name: 'Register',
  data() {
    // eslint-disable-next-line no-unused-vars
    const validateRepass = (rule, value, callback) => {
      if (value !== this.regForm.password) {
        callback(new Error('两次密码不一样'))
      } else {
        callback()
      }
    }
    return {
      regForm: {
        username: '',
        // eslint-disable-next-line quotes
        password: '',
        // eslint-disable-next-line quotes
        repassword: ''
      },
      regFormRules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            pattern: /^[a-z\d]{1,10}$/i,
            // eslint-disable-next-line quotes
            message: '用户名由数字字母组成，长度1-10',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            pattern: /^\S{6,15}$/i,
            // eslint-disable-next-line quotes
            message: '密码由6-15位的非空格',
            trigger: 'blur'
          }
        ],
        repassword: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            pattern: /^\S{6,15}$/i,
            // eslint-disable-next-line quotes
            message: '再次输入密码',
            trigger: 'blur'
          },
          { validator: validateRepass, trigger: blur }
        ]
      }
    }
  },
  methods: {
    handleReg() {
      this.$refs.regFormRef.validate(async (valid) => {
        if (!valid) return
        const {
          data: { code, message }
          // eslint-disable-next-line no-undef
        } = await regAPI(this.regForm)
        if (code === 0) {
          this.$message.success(message)
          this.$router.push('/login')
        } else {
          this.$message.warning(message)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.register-container {
  background: url('~@/assets/images/login_bg.jpg') center;
  background-size: cover;
  height: 100%;

  .register-box {
    width: 400px;
    height: 405px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 0 30px;
    box-sizing: border-box;

    .title-box {
      height: 60px;
      background: url('~@/assets/images/login_title.png') center no-repeat;
    }

    .el-button {
      width: 100%;
    }
  }
}
</style>
